import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import createPersistedstate from 'vuex-persistedstate'
// Vuex实际上是Vue的插件 
Vue.use(Vuex)
                                                                                        
const state = {
    count: 0,
    price: 45,
    allPrice: 0,
    movieList: [],
    searchMovieList:[]
}

const mutations = {
    increatment(state) {
        state.count++
        state.allPrice = state.count * state.price
    },
    setMovieList(state, re) {
        state.movieList = re.data.data.list
    },
    setsearchMovie(state,{search,re}){
        state.searchMovieList = re.data.data.list
        state.searchMovieList=state.searchMovieList.filter(item=>{
            return item.albumName.includes(search) ||item.categories.includes(search) || item.people.main_charactor.includes(search)
        })

    },
    resetMovieList(state){
        state.searchMovieList=[];
    }
}

const actions = {
    async getMovieList({ commit }) {
        const re = await axios.get('https://pcw-api.iqiyi.com/search/recommend/list?channel_id=1&data_type=1&mode=11&page_id=2&ret_num=48&session=b9fd987164f6aa47fad266f57dffaa6a')
        commit('setMovieList', re)
        
    },
    async getsearchMovieList({ commit },search) {
        const re = await axios.get('https://pcw-api.iqiyi.com/search/recommend/list?channel_id=1&data_type=1&mode=11&page_id=2&ret_num=48&session=b9fd987164f6aa47fad266f57dffaa6a')
        commit('setsearchMovie',{search,re})
    },

}

// getters和vue的计算属性相似
const getters = {
    myMovieList(state){
        // console.log(1111);
        return state.movieList.filter(item=>{
            return item.name.length>5 && item.name.length<=8
        })
    },
   
    
}

// 创建一个Store Store实际上是Vuex上的一个实例
export default new Vuex.Store({
    state: state,
    mutations: mutations,
    actions: actions,
    getters:getters,
    plugins:[createPersistedstate({})]
})